<!DOCTYPE html>
<html>

	<head>
		<title>【在线订餐系统】— 后台管理</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type='text/css' href='/css/admin/admin.css' />
		<style>
		</style>
	</head>

	<body>

		<div class="header">欢迎登录后台管理系统
			<div class="user-exit floatright">
				<span><%=user.username%></span>
				<a href="/admin/logout">退出</a>
			</div>
		</div>
		<div class="topmenu">
			<ul>
				<a href="/admin/index">
					<li class="current-item">用户管理</li>
				</a>
				<a href="#">
					<li>分类管理</li>
				</a>
				<a href="#">
					<li>新闻管理</li>
				</a>
				<a href="#">
					<li>关于我们</li>
				</a>
			</ul>
		</div>
		<div class="split-line"></div>
		<div class="main">
			<center>
				<h1>编辑用户</h1>
				<span id="error" class="error"></span>
				<form method="post" action="/">
					<table class="table-adduser">
						<tr>
							<td>
								<span>user_id:</span>
							</td>
							<th>
								<%=edituser.user_id%>
							</th>
						</tr>
						<tr>
							<td>
								<span>user_name:</span>
							</td>
							<th>
								<input type="text" id="username" name="username" value="<%=edituser.user_name%>">
							</th>
						</tr>
						<tr>
							<td>
								<span>user_sex:</span>
							</td>
							<th>
								<input type="text" id="usersex" name="username" value="<%=edituser.user_sex%>">
							</th>
						</tr>
						<tr>
							<td>
								<span>user_age:</span>
							</td>
							<th>
								<input type="text" id="userage" name="username" value="<%=edituser.user_age%>">
							</th>
						</tr>
						<tr>
							<td>
								<span>user_phone:</span>
							</td>
							<th>
								<input type="text" id="userphone" name="username" value="<%=edituser.user_phone%>">
								<span class="error">*</span>
							</th>
						</tr>
						<tr>
							<td>
								<span>user_nick:</span>
							</td>
							<th>
								<input type="text" id="usernick" name="username" value="<%=edituser.user_nick%>">
							</th>
						</tr>
						<tr>
							<td>
								<span>user_pwd:</span>
							</td>
							<th>
								<input type="password" id="userpwd" name="username" value="<%=edituser.user_pwd%>">
								<span class="error">*</span>
							</th>
						</tr>
						<tr>
							<td>
								<span>is_admin:</span>
							</td>
							<th>
								<input type="text" id="isadmin" name="username" value="<%=edituser.is_admin%>">
							</th>
						</tr>
					</table>
					<input type="button" id="submit_btn" value="提交">
				</form>

			</center>
		</div>

		<script src="/js/common/jquery-1.12.4.min.js"></script>
		<script>
			$(document).keydown(function(event) {
				if(event.keyCode == 13) {
					checkForm();
				}
			});

			$("#submit_btn").click(function() {
				checkForm()
			})

			$("#userphone").blur(function() {
				if(this.value.trim() == '') {
					$("#error").html("请输入phone");
				}
			})

			$("#userpwd").blur(function() {
				if(this.value.trim() == '') {
					$("#error").html("请输入pwd");
				}
			})

			function checkForm() {
				if($('#submit_btn').attr('disabled') == true) {
					return false;
				}
				
				var userid = <%=edituser.user_id%>;
				var username = $("#username").val().trim();
				var usersex = $("#usersex").val().trim();
				var userage = $("#userage").val().trim();
				var userphone = $("#userphone").val().trim();
				var usernick = $("#usernick").val().trim();
				var userpwd = $("#userpwd").val().trim();
				var isadmin = $("#isadmin").val().trim();

				if(userphone == '') {
					$("#error").html("请输入phone");
					$('#submit_btn').removeAttr('disabled');
					return;
				}

				if(userpwd == '') {
					$("#error").html("请输入pwd");
					$('#submit_btn').removeAttr('disabled');
					return;
				}

				$("#error").html("正在添加，请稍后...");
				$('#submit_btn').attr('disabled', true);

				$.ajax({
					type: 'post',
					url: "/admin/edituser",
					timeout: 3000,
					dataType: 'json',
					data: {
						userid: userid,
							username: username,
							usersex: usersex,
							userage: userage,
							userphone: userphone,
							usernick: usernick,
							userpwd: userpwd,
							isadmin: isadmin
					},
					success: function(result) {
						console.log(result);
						$("#error").html(result.msg);
						$('#submit_btn').removeAttr('disabled');
						if(result.err == 0 && result.data.url) {
							window.location.href = result.data.url;
						}
					},
					error: function(error) {
						console.log(error);
						$('#submit_btn').removeAttr('disabled');
						$("#error").html("请求出错，请稍后再试");
					}
				});
			}
		</script>
	</body>

</html>